/*
 * @Author: maskMan
 * @LastEditTime: 2024-03-27 19:54:53
 * @Descripttion:回收池
 */
import { AssetManager, assetManager, AudioClip, Component, ImageAsset, JsonAsset, Material, Prefab, SpriteFrame, Texture2D } from 'cc'
import { DIR_KEY } from '../define/constants'
export default class ResManager extends Component {
  static _bundle: AssetManager.Bundle
  /**加载包 */
  static loadBundle(): Promise<boolean> {
    return new Promise((resolve) => {
      assetManager.loadBundle(DIR_KEY.BUNDLE, null, (err, bundle) => {
        if (!err) {
          ResManager._bundle = bundle
          return resolve(true)
        }
        ResManager._bundle = null
        return resolve(false)
      })
    })
  }
  /**加载包路径资源 */
  static loadDirAsset(onProgress, onFinished) {
    ResManager._bundle.loadDir(DIR_KEY.BUNDLE, null, onProgress, onFinished)
  }
  /**远程加载图片 */
  static loadRemote(url: string, ext: '.png' | '.jpg' = '.png'): Promise<SpriteFrame> {
    return new Promise(() => {
      assetManager.loadRemote<ImageAsset>(url, { ext }, (err, imageAsset) => {
        if (!err) {
          const tex = new Texture2D()
          tex.image = imageAsset
          const sp = new SpriteFrame()
          sp.texture = tex
          return sp
        }
        console.error(err)
        return null
      })
    })
  }
  /**加载预制体 */
  static loadPrefab(name?: string, dir: DIR_KEY.PREPUB | DIR_KEY.PREGAME = DIR_KEY.PREGAME): Promise<Prefab | Prefab[]> {
    return new Promise(() => {
      ResManager._bundle.load<Prefab>(`${dir}/${name ? name : ''}`, Prefab, (err, pre) => {
        if (!err) {
          return pre
        }
        console.error(err)
        return null
      })
    })
  }
  /**加载精灵图片 */
  static loadSpriteFrame(name?: string, dir: DIR_KEY.LOADSP | DIR_KEY.BASETEXTURE = DIR_KEY.LOADSP): Promise<SpriteFrame | SpriteFrame[]> {
    return new Promise(() => {
      ResManager._bundle.load<SpriteFrame>(`${dir}/${name ? name : ''}`, SpriteFrame, (err, sp) => {
        if (!err) {
          return sp
        }
        console.error(err)
        return null
      })
    })
  }
  /**加载纹理 */
  static loadTexture(name?: string, dir: DIR_KEY.BASETEXTURE | DIR_KEY.LOADTEX = DIR_KEY.LOADTEX): Promise<Texture2D | Texture2D[]> {
    return new Promise(() => {
      ResManager._bundle.load<Texture2D>(`${dir}/${name ? name : ''}`, Texture2D, (err, tex) => {
        if (!err) {
          return tex
        }
        console.error(err)
        return null
      })
    })
  }
  /**加载音乐/音效 */
  static loadAudio(name?: string, dir: DIR_KEY.MUSIC | DIR_KEY.SOUND = DIR_KEY.SOUND): Promise<AudioClip | AudioClip[]> {
    return new Promise(() => {
      ResManager._bundle.load<AudioClip>(`${dir}/${name ? name : ''}`, AudioClip, (err, tex) => {
        if (!err) {
          return tex
        }
        console.error(err)
        return null
      })
    })
  }
  /**加载json资源 */
  static loadJson(name?: string, dir: DIR_KEY.JSON = DIR_KEY.JSON): Promise<JsonAsset | JsonAsset[]> {
    return new Promise(() => {
      ResManager._bundle.load<JsonAsset>(`${dir}/${name ? name : ''}`, JsonAsset, (err, tex) => {
        if (!err) {
          return tex
        }
        console.error(err)
        return null
      })
    })
  }
  /**加载Material材质资源 */
  static loadMaterial(name?: string, dir: DIR_KEY.MATERIAGAME | DIR_KEY.MATERIAPUB = DIR_KEY.MATERIAPUB): Promise<Material | Material[]> {
    return new Promise(() => {
      ResManager._bundle.load<Material>(`${dir}/${name ? name : ''}`, Material, (err, tex) => {
        if (!err) {
          return tex
        }
        console.error(err)
        return null
      })
    })
  }
}
